# Extreme weather events do not increase political parties' environmental attention

#'   This script
#'      > cleans press release data into party*day form with
#'      > *N* PRs across all *issues*
#'      > *N* PRs from climate dictionary
#'      > *Party Characteristics*
#'      > *Extreme weather events*


# Tim Wappenhans, António Valentim, Heike Klüver, and Lukas F. Stoetzer
# First: 18-02-2024
# Last: 23-02-2024

# PACKAGES --------------------------------------------------------------------
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
  tidyverse,
  fixest,
  broom,
  here
)


# DATA WRANGLING --------------------------------------------------------------

# check for doubles (multiple events wihtin 6 weeks)
doubles <- read_csv(here("data/data_output/press_weekly.csv"))  |> 
  # event on country level
  group_by(country_name, t) |> 
  summarize(treat = mean(treat, na.rm =T)) |> 
  # only keep treated weeks
  filter(treat == 1) |> 
  # create indicator for difference between last and next treated time point
  mutate(lagged_double = t - lag(t, default = 100),
         lead_double = lead(t, default = 100) - t) |> 
  ungroup() |> 
  # only keep doubles
  filter(lagged_double <= 6 & lagged_double > 0,
         lead_double <= 6 & lead_double > 0) |> 
  # get country_id for these events
  mutate(cntry_id = paste0(country_name, "_", t)) |> 
  pull(cntry_id)


df.robust <- read_csv(here("data/data_output/press_weekly.csv"))  |> 
  
  mutate(
    cntry_id = paste0(country_name, "_", t),
    outcome = issue_7 / pr_total * 100,
    outcome = ifelse(is.nan(outcome), 0, outcome) 
    
  ) |> 
  
  # filter out doubles
  filter(!cntry_id %in% doubles) |> 
  
  # create lead and lag treatment indicators
  group_by(parlgov_id) |> 
  mutate(
    
    # create indicators for periods since treatment
    d0 = ifelse(treat == 1, 1, 0),
    
    # take first week as treatment in case event spreads across multiple weeks
    d0 = ifelse(lag(d0 == 1, default = 0), 0, d0), 
    
    # lags
    lag1 = ifelse(lag(d0 == 1, default = 0), 1, 0),
    lag2 = ifelse(lag(lag1 == 1, default = 0), 1, 0),
    lag3 = ifelse(lag(lag2 == 1, default = 0), 1, 0),
    lag4 = ifelse(lag(lag3 == 1, default = 0), 1, 0),
    lag5 = ifelse(lag(lag4 == 1, default = 0), 1, 0),
    lag6 = ifelse(lag(lag5 == 1, default = 0), 1, 0),
    
    # leads
    lead1 = ifelse(lead(d0 == 1, default = 0), 1, 0),
    lead2 = ifelse(lead(lead1 == 1, default = 0), 1, 0),
    lead3 = ifelse(lead(lead2 == 1, default = 0), 1, 0),
    lead4 = ifelse(lead(lead3 == 1, default = 0), 1, 0),
    lead5 = ifelse(lead(lead4 == 1, default = 0), 1, 0),
    lead6 = ifelse(lead(lead5 == 1, default = 0), 1, 0)
    
  ) |> 
  
  # get rid of all columns for different issues
  select(
    !starts_with("issue_")
  ) 



# get treated units
df.treated <- df.robust |> 
  
  # filter 8 weeks before and after event
  filter(
    d0 == 1 |
      lead1 == 1 |
      lead2 == 1 |
      lead3 == 1 |
      lead4 == 1 |
      lead5 == 1 |
      lead6 == 1 |
      lag1 == 1 |
      lag2 == 1 |
      lag3 == 1 |
      lag4 == 1 |
      lag5 == 1 |
      lag6 == 1 
  ) 



# pull weeks to observe 
t_weeks <- df.treated |> 
  pull(t) |> 
  unique()

# observe same weeks as in actually treated
df.placebo <- df.robust |> filter(t %in% t_weeks)

# filter out actually treated units
df.placebo <- anti_join(df.placebo, df.treated)


# pull treatment timing
t_placebo_nolag <- df.treated |> 
  filter(d0 == 1) |> 
  group_by(t) |> 
  summarize(t = mean(t)) |> 
  ungroup() |> 
  # if multiple treatments within 6 weeks: only keep first
  mutate(lag_t = t - lag(t)) |> 
  filter(lag_t > 6) |> 
  pull(t)


# create placebo df
df.placebo <- df.placebo |> 
  
  # placebo treatment
  group_by(parlgov_id) |> 
  mutate(
    d0 = ifelse(t %in% t_placebo_nolag, 1, 0)) |> 
  
  # leads and lags
  group_by(parlgov_id) |> 
  mutate(
    # lags
    lag1 = ifelse(lag(d0 == 1, default = 0), 1, 0),
    lag2 = ifelse(lag(lag1 == 1, default = 0), 1, 0),
    lag3 = ifelse(lag(lag2 == 1, default = 0), 1, 0),
    lag4 = ifelse(lag(lag3 == 1, default = 0), 1, 0),
    lag5 = ifelse(lag(lag4 == 1, default = 0), 1, 0),
    lag6 = ifelse(lag(lag5 == 1, default = 0), 1, 0),

    # leads
    lead1 = ifelse(lead(d0 == 1, default = 0), 1, 0),
    lead2 = ifelse(lead(lead1 == 1, default = 0), 1, 0),
    lead3 = ifelse(lead(lead2 == 1, default = 0), 1, 0),
    lead4 = ifelse(lead(lead3 == 1, default = 0), 1, 0),
    lead5 = ifelse(lead(lead4 == 1, default = 0), 1, 0),
    lead6 = ifelse(lead(lead5 == 1, default = 0), 1, 0)
  )



# MODELING ---------------------------------------------------------------------

m_placebo_fe = feols(outcome ~ 
                       t + d0 + lag1 + lag2 + lag3 + lag4 + lag5 + lag6 | 
                       parlgov_id, df.placebo)




# store estimates
gg.placebo <- data.frame(
  coef = coef(m_placebo_fe)[c("d0", "lag1", "lag2", "lag3", "lag4", "lag5", "lag6")],
  se = se(m_placebo_fe)[c("d0", "lag1", "lag2", "lag3", "lag4", "lag5", "lag6")]
)


# period + CIs
gg.placebo <- gg.placebo |> 
  mutate(
    period = row_number() - 1,
    
    # CIs
    low_ci = coef - 1.96*se,
    up_ci  = coef + 1.96*se,
  )

# PLOT -------------------------------------------------------------------------
gg.placebo |> 
  ggplot() + 
  
  # Gray pre treatment shade
  geom_rect(aes(xmin = -Inf, xmax = -0.5, ymin = -Inf, ymax = Inf),
            fill = "#e0e0e0", alpha = 0.2) +
  
  # CIs
  geom_linerange(aes(x = period, ymin = low_ci, ymax = up_ci),
                 alpha = .7,
                 linewidth = 0.75) + 
  
  # fixed lines
  geom_hline(yintercept = 0, linetype = 'dotted') + 
  
  
  # CIs
  geom_linerange(aes(x = period, ymin = low_ci, ymax = up_ci),
                 linewidth = 0.5) + 
  
  # Coef
  geom_point(aes(x = period, y = coef),
             size = 3)  +
  
  # fixed lines
  geom_hline(yintercept = 0, linetype = 'dotted') + 
  
  # look
  ylab("Change in issue attention to the environment") +
  xlab("Weeks relative to placebo weather event") + 
  
  # theme
  theme_bw() +
  theme(panel.grid = element_blank(),
        plot.title = element_text(hjust = 0.5)) 
  

# EXPORT -----------------------------------------------------------------------
ggsave(here("results/graphs/fig_ex_data_03.pdf"),
       height = 4, 
       width = 5)